﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform.Controls
{
    /// <summary>
    /// 消息格式
    /// </summary>
    public enum MessageStyle
    {
        Normal = 1,
        Title1 = 2,  // 标题1
        Title2 = 4,  // 标题2
        Color1 = 8,  // 红色
        Color2 = 16   // 蓝色
    }

    public partial class LogWindow : FloatableWindow
    {
        #region 字段
        private static LogWindow _singleton;
        private static readonly Queue<Message> _logCache = new Queue<Message>();
        #endregion

        #region 函数
        private LogWindow()
        {
            InitializeComponent();
            this.Unloaded += DebugWindow_Unloaded;
        }

        private void DebugWindow_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Unloaded -= DebugWindow_Unloaded;
            _singleton = null;
        }

        protected override void OnOpened()
        {
            base.OnOpened();
            _singleton = this;

            while (_logCache.Count > 0)
            {
                AddMessage(_logCache.Dequeue());
            }
        }

        public static void AddLog(string message)
        {
            AddLog(message, MessageStyle.Normal);
        }

        public static void AddLog(string message, MessageStyle style)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            _logCache.Enqueue(new Message(message, style));

            if (_singleton == null)
            {
                return;
            }

            while (_logCache.Count > 0)
            {
                AddMessage(_logCache.Dequeue());
            }
        }

        private static void AddMessage(Message message)
        {
            Run run = new Run();
            run.Text = message.Log;

            if ((message.Style & MessageStyle.Title1) == MessageStyle.Title1)
            {
                run.FontSize = 16;
            }

            if ((message.Style & MessageStyle.Title2) == MessageStyle.Title2)
            {
                run.FontSize = 14;
            }

            if ((message.Style & MessageStyle.Color1) == MessageStyle.Color1)
            {
                run.Foreground = new SolidColorBrush(Colors.Red);
            }

            if ((message.Style & MessageStyle.Color2) == MessageStyle.Color2)
            {
                run.Foreground = new SolidColorBrush(Colors.Yellow);
            }

            _singleton.MainParagraph.Inlines.Add(run);
            _singleton.MainParagraph.Inlines.Add(new LineBreak());
        }

        public static void AddLine()
        {
            _logCache.Enqueue(new Message(string.Empty, MessageStyle.Normal));

            if (_singleton == null)
            {
                return;
            }

            while (_logCache.Count > 0)
            {
                AddMessage(_logCache.Dequeue());
            }
        }

        public static void Show(Panel panel)
        {
            _singleton = new LogWindow();
            _singleton.ParentLayoutRoot = panel;
            _singleton.Title = "日志信息";
            _singleton.ResizeMode = ResizeMode.CanResize;
            _singleton.Show();
        }
        #endregion

        private class Message
        {
            private string _log;
            private MessageStyle _style;

            public string Log
            {
                get
                {
                    return _log;
                }
            }

            public MessageStyle Style
            {
                get
                {
                    return _style;
                }
            }

            public Message(string log, MessageStyle style)
            {
                _log = log;
                _style = style;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainParagraph.Inlines.Clear();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (Inline inline in MainParagraph.Inlines)
                {
                    if (inline is Run)
                    {
                        sb.Append(((Run)inline).Text);
                    }
                    else if (inline is LineBreak)
                    {
                        sb.AppendLine();
                    }
                }

                Clipboard.SetText(sb.ToString());
                MessageBox.Show("已经复制到剪切板。");

            }
            catch (Exception ex)
            {
                MessageBox.Show("必须允许此Web应用程序访问您的剪切板，才能复制位置信息。", "提示", MessageBoxButton.OK);
            }

        }
    }
}
